package com.example.zhidao.config;

import com.example.zhidao.Interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Resource
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/banner")
                .addPathPatterns("/blog/admin/delBlog")
                .addPathPatterns("/blog/adminGetBlog")
                .addPathPatterns("/cgoods/add")
                .addPathPatterns("/cgoods/del")
                .addPathPatterns("/chat/infoList")
                .addPathPatterns("/chat/list")
                .excludePathPatterns("/imserver/*")
                .addPathPatterns("/blog/del")//文章删除
                .addPathPatterns("/blog/update")//文章修改
                .addPathPatterns("/blog/add")//文章增加
                .addPathPatterns("/user/info")//获取用户信息
                .addPathPatterns("/blog/my") // 获取登录用户本人已上线的博客
                .addPathPatterns("/blog/my/await") // 获取登录用户本人未上线的博客
                .addPathPatterns("/fans/add")//关注
                .addPathPatterns("/fans/del")//不再关注
                 .addPathPatterns("/comments/one")//一级评论
                .addPathPatterns("/comments/two")//二级评论
                 .addPathPatterns("/goods/add")//点赞
                .addPathPatterns("/goods/del")//删除点赞
                 .addPathPatterns("/collections/add")//收藏
                  .addPathPatterns("collections/del")//取消收藏
                .addPathPatterns("views/addViews");//增加观看次数
    }



//    开发过程中发现的问题，之前注解跨域支队
    //配置跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
    private CorsConfiguration addCorsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        List<String> list = new ArrayList<>();
        list.add("*");
        corsConfiguration.setAllowedOrigins(list);
    /*
    // 请求常用的三种配置，*代表允许所有，当时你也可以自定义属性（比如header只能带什么，只能是post方式等等）
    */
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", this.addCorsConfig());
        return new CorsFilter(source);
    }


}
